package com.joekai.base_library.base;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.util.List;

/**
 * author:  JoeKai
 * date: 2022/7/4 22:39
 * content：
 */
public abstract class BaseLazyFragment extends Fragment {
    View rootView;
    boolean isViewCreated;//根布局是否创建
    boolean currentVisibleState;//当前View可见状态
    private boolean isLoadData = false;//数据是否加载
    private boolean isFirstVisible = true;//是否第一次可见
    public String tag = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        E("onCreateView");
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        initView(savedInstanceState);
        isViewCreated = true;
        if (getUserVisibleHint()) {
            dispatchUserVisibleHint(true);
        }
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected abstract int getLayoutId();

    protected abstract void initView(Bundle bundle);

    //判断fragment是否可见
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isViewCreated) {
            if (isVisibleToUser && !currentVisibleState) {
                dispatchUserVisibleHint(true);
            } else {
                dispatchUserVisibleHint(false);
            }
        }
    }

    /**
     * 分发数据加载与停止加载事务
     *
     * @param isVisible
     */
    private void dispatchUserVisibleHint(boolean isVisible) {
        if (currentVisibleState == isVisible) {
            return;
        }
        currentVisibleState = isVisible;
        if (isVisible) {
            onFragmentLoad();
            dispatchChildVisibleState(true);
        } else {
            onFragmentLoadStop();
            dispatchChildVisibleState(false);
        }

    }

    private void dispatchChildVisibleState(boolean isVisible) {

        FragmentManager childFragmentManager = getChildFragmentManager();
        List<Fragment> childFragmentManagerFragments = childFragmentManager.getFragments();
        if (childFragmentManagerFragments != null) {
            for (Fragment fragment : childFragmentManagerFragments) {
                if (fragment instanceof BaseLazyFragment && !fragment.isHidden()&&fragment.getUserVisibleHint()) {
                    ((BaseLazyFragment) fragment).dispatchUserVisibleHint(isVisible);
                }
            }
        }

    }

    //停止加载
    public void onFragmentLoadStop() {
        E("onFragmentLoadStop");

    }

    //加载
    public void onFragmentLoad() {
        E("onFragmentLoad");
    }

    @Override
    public void onResume() {
        super.onResume();
        E("onResume");
        if (currentVisibleState && getUserVisibleHint()) {
            dispatchUserVisibleHint(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        E("onPause");
        if (!currentVisibleState && getUserVisibleHint()) {
            dispatchUserVisibleHint(false);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        E("onDestroyView");
        isViewCreated = false;
    }

    public void setFragmentDelegate() {
    }

    void E(String s) {
        Log.e("----" + tag, s);
    }
}
